package com.pzb.web.controller.goods;

import java.util.List;

import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author Mr_Wu
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/goods/goods")
public class GdGoodsController extends BaseController
{
    @Autowired
    private IGdGoodsService gdGoodsService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(GdGoods gdGoods)
    {
        startPage();
        gdGoods.setExtAppid(wechatAppletService.getExtAppid());
        List<GdGoods> list = gdGoodsService.selectGdGoodsList(gdGoods);
        return getDataTable(list);
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gdGoodsService.selectGdGoodsById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GdGoods gdGoods)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        gdGoods.setExtAppid(extAppid);
        return toAjax(gdGoodsService.insertGdGoods(gdGoods));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GdGoods gdGoods)
    {
        return toAjax(gdGoodsService.updateGdGoods(gdGoods));
    }


    /**
     * 修改商品状态
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeGoodsStatus(@RequestBody GdGoods gdGoods)
    {
        return toAjax(gdGoodsService.changeGoodsStatus(gdGoods));
    }

    /**
     * 修改商品状态
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/batchChangeGoodsStatus")
    public AjaxResult batchChangeGoodsStatus(@RequestBody GdGoods gdGoods)
    {
        return toAjax(gdGoodsService.batchChangeGoodsStatus(gdGoods));
    }


    /**
     * 编辑价格库存
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/editPriceStock")
    public AjaxResult editPriceStock(@RequestBody GdGoods gdGoods)
    {
        return toAjax(gdGoodsService.updateGoodsPriceStock(gdGoods));
    }
    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('goods:goods:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gdGoodsService.deleteGdGoodsByIds(ids));
    }
}
